import argparse
import os
from datetime import datetime
from pathlib import Path

def parse_arguments():
    parser = argparse.ArgumentParser(description="Process command-line arguments for the script.")

    # Optional arguments
    parser.add_argument("--c_project_path", type=str, help="Path to the C project.")
    parser.add_argument("--rust_output_path", type=str, help="Path to the Rust output directory.")

    # Other optional arguments
    parser.add_argument("--rounds", default=5, type=int, help="Number of fix rounds (default: 5).")
    parser.add_argument("--log_path", default="./log", type=str, help="Path to the log file (default: ./log).")
    parser.add_argument("--temp_path", default="./temp", type=str, help="Path to the temp file (default: ./temp).")
    parser.add_argument("--parallel_file", default="20", type=int)
    parser.add_argument("--parallel_unit", default="20", type=int)

    parsed_args = parser.parse_args()

    return parsed_args

if __name__ == "__main__":
    args = parse_arguments()

    # Print parsed arguments (for demonstration)
    print("C Project Path:", args.c_project_path)
    print("Rust Output Path:", args.rust_output_path)
    print("Rounds:", args.rounds)
    print("Log Path:", args.log_path)

    begin = datetime.now()

    log_path = Path(args.log_path)
    temp_path = Path(args.temp_path)
    rust_path = Path(args.rust_output_path)

    if rust_path.exists():
        raise FileExistsError("The Rust output path already exists.")
    if temp_path.exists():
        raise FileExistsError("The temp path already exists.")
    if log_path.exists():
        raise FileExistsError("The log path already exists.")

    log_path.mkdir(parents=True, exist_ok=True)
    temp_path.mkdir(parents=True, exist_ok=True)
    rust_path.mkdir(parents=True, exist_ok=True)
    os.environ['LOG_PATH'] = str(log_path.resolve())

    from framework.translator import Translator

    translator = Translator(c_project_path=args.c_project_path,
                            rust_project_path=rust_path,
                            temp_path=temp_path,
                            parallel_file=args.parallel_file,
                            parallel_unit=args.parallel_unit)

    translator.run()

    end = datetime.now()
    print(end - begin)
